package cn.calm.repaymentNotice.client;

import java.util.Calendar;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
import cn.calm.repaymentNotice.R;
import cn.calm.repaymentNotice.dao.DBService;
import cn.calm.repaymentNotice.entity.CreditCardRecord;
import cn.calm.repaymentNotice.entity.RepaymentNotice;
import cn.calm.repaymentNotice.entity.RepaymentNoticeInfo;

public class EditNoticeActivity extends Activity {
	private RepaymentNotice notice;
	private EditText name ;
	private EditText pre_repayment_day ;
	private Spinner type;
	private Spinner repaymentDate;
	private DBService dao;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_add_notice);
		Intent intent = getIntent();
		long noticeId=intent.getLongExtra("notice_id", -1);
		type = (Spinner) findViewById(R.id.notice_type);
		if(noticeId == -1){
			notice = new RepaymentNotice();
		}else{
			notice = getDbService().getRepaymentNotice(noticeId);
			type.setEnabled(false);
		}
		type.setSelection(notice.noticeType);
		final Button pass = (Button) findViewById(R.id.pass);
		name = (EditText) findViewById(R.id.name);
		name.setText(notice.name);
		
		pre_repayment_day=(EditText) findViewById(R.id.pre_repayment_day);
		if(notice.preNoticeDays>0){
			pre_repayment_day.setText(notice.preNoticeDays+"");
		}
		repaymentDate=(Spinner) findViewById(R.id.repayment_day);
		repaymentDate.setSelection(notice.repaymentDate-1);
		type.setOnItemSelectedListener(new OnItemSelectedListener() {

			public void onItemSelected(AdapterView<?> parent, View view,
					int position, long id) {
				if (position == 0) {
					pass.setText("保存");
				} else {
					pass.setText("下一步");
				}
			}

			public void onNothingSelected(AdapterView<?> parent) {

			}
		});

		pass.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				try {
					validate();
					int pos = type.getSelectedItemPosition();
					if (pos == 0) {
						saveCard();
					} else {
						Intent intent=new Intent();
						intent.putExtra("info", notice);
						intent.setClass(EditNoticeActivity.this, CreditPlanActivity.class);
						startActivity(intent);
					}
				} catch (RepaymentNoticeException e) {
					Toast.makeText(getApplicationContext(),e.getMessage(),
							Toast.LENGTH_LONG).show();
				}
			}
		});
	}

	protected void validate() throws RepaymentNoticeException {
		StringBuilder strBu=new StringBuilder();
		String nameStr=name.getText().toString().trim();
		if(nameStr.length()==0){
			strBu.append("请输入名称!\n");
		}
		
		String preRepaymentDayStr=pre_repayment_day.getText().toString().trim();
		int days = 0;
		if(preRepaymentDayStr.length()>0){
			days=Integer.valueOf(preRepaymentDayStr);
			if(days<0){
				strBu.append("提前提醒天数请输入大于0的数字!\n");
			}
		}else{
			strBu.append("请输入提前提醒天数!\n");
		}
		if(strBu.length()>0){
			throw new RepaymentNoticeException(strBu.deleteCharAt(strBu.length()-1).toString());
		}
		notice.noticeType = type.getSelectedItemPosition();
		notice.name = name.getText().toString();
		if(notice.id==1){
			getDbService().validate(notice);
		}
		notice.preNoticeDays=days;
		notice.repaymentDate=repaymentDate.getSelectedItemPosition()+1;
	}
	
	private DBService getDbService(){
		if(dao == null)
		{
			dao=DBService.getInstance(getApplicationContext());
		}
		return dao;
	}

	private void saveCard() throws RepaymentNoticeException {
		Calendar today=Calendar.getInstance();
		
		int day = today.get(Calendar.DAY_OF_MONTH);
		if(day > notice.repaymentDate){
			today.add(Calendar.MONTH, 1);
		}
		today.set(Calendar.DAY_OF_MONTH, notice.repaymentDate);
		CreditCardRecord record=new CreditCardRecord();
		
		record.predictRepaymentDate=today.getTime();
		
		if(notice.id == -1){
			getDbService().saveRepaymentNotice(notice);
			record.repaymentNoticeId = notice.id;
			getDbService().saveCreditCardRecord(record,null);
		}else{
			RepaymentNoticeInfo tempInfo=getDbService().getRepaymentNoticeInfo(notice.id);
			getDbService().saveRepaymentNotice(notice);
			record.repaymentNoticeId = notice.id;
			getDbService().saveCreditCardRecord(record,tempInfo.predictRepaymentDate);
		}
		Intent intent=new Intent();
		intent.setClass(EditNoticeActivity.this, MainActivity.class);
		startActivity(intent);
	}
}
